﻿
Imports xmlctrl
Imports webctrl

Public Class ie8acc
    Inherits cacheControlProcessor

    'this can only serve one accelerator in one server,
    'but if needed, i can add a map here and some url parameter to select
    'seems not neccessary now

    Public Const _pathidentity As String = "/ie8acc"
    Private info As ie8acceleratorInfo = Nothing

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Private Shared Sub parameters(ByVal root As xmlcontrol, _
                                  ByVal context As httpContext, _
                                  ByVal execute As ie8acceleratorInfo.execute, _
                                  ByVal baseUrl As String)
        assert(Not root Is Nothing, "should have a valid root control.")
        assert(Not context Is Nothing, "should have a valid httpContext.")
        assert(Not execute Is Nothing, "should have a valid ie8acceleratorInfo.execute.")

        If Not execute.parameters Is Nothing AndAlso execute.parameters.size() > 0 Then
            Dim i As Int64
            For i = 0 To execute.parameters.size() - 1
#If RETIRED Then
                Dim parameter As ie8accParameter = Nothing
                parameter = context.createControl(New ie8accParameter())
#End If
                Dim parameter As xmlcontrol = Nothing
                parameter = context.createControl(New xmlcontrol(Nothing, "parameter"))
                parameter.attributes("name") = execute.parameters(i).name
                parameter.attributes("value") = execute.parameters(i).value
                If Not isemptystring(execute.parameters(i).type) Then
                    parameter.attributes("type") = execute.parameters(i).type
                End If
                root.addchild(parameter)
            Next
        End If
    End Sub

    Private Shared Sub executes(ByVal root As xmlcontrol, _
                                ByVal context As httpContext, _
                                ByVal action As ie8acceleratorInfo.action, _
                                ByVal baseUrl As String)
        assert(Not root Is Nothing, "should have a valid root control.")
        assert(Not context Is Nothing, "should have a valid httpContext.")
        assert(Not action Is Nothing, "should have a valid ie8acceleratorInfo.action.")

        assert(Not action.executes Is Nothing AndAlso action.executes.size() > 0, "no valid executes.")
        Dim i As Int64
        For i = 0 To action.executes.size() - 1
#If RETIRED Then
            Dim execute As ie8accExecute = Nothing
            execute = context.createControl(New ie8accExecute())
#End If
            Dim execute As xmlcontrol = Nothing
            execute = context.createControl(New xmlcontrol(Nothing, "execute"))
            If Not isemptystring(action.executes(i).method) Then
                execute.attributes("method") = action.executes(i).method
            End If
            execute.attributes("action") = baseUrl + action.executes(i).actionPath
            root.addchild(execute)

            parameters(execute, context, action.executes(i), baseUrl)
        Next
    End Sub

    Private Shared Sub actions(ByVal root As xmlcontrol, _
                               ByVal context As httpContext, _
                               ByVal activity As ie8acceleratorInfo.activity, _
                               ByVal baseUrl As String)
        assert(Not root Is Nothing, "should have a valid root control.")
        assert(Not context Is Nothing, "should have a valid httpContext.")
        assert(Not activity Is Nothing, "should have a valid ie8acceleratorInfo.activity.")

        assert(Not activity.actions Is Nothing AndAlso activity.actions.size() > 0, "no valid actions.")
        Dim i As Int64
        For i = 0 To activity.actions.size() - 1
            Dim action As xmlcontrol = Nothing
            action = context.createControl(New xmlcontrol(Nothing, "activityAction"))
            action.attributes("context") = activity.actions(i).context
            root.addchild(action)

            executes(action, context, activity.actions(i), baseUrl)
        Next
    End Sub

    Private Shared Sub activities(ByVal root As xmlcontrol, _
                                  ByVal context As httpContext, _
                                  ByVal info As ie8acceleratorInfo, _
                                  ByVal baseUrl As String)
        assert(Not root Is Nothing, "should have a valid root control.")
        assert(Not context Is Nothing, "should have a valid httpContext.")
        assert(Not info Is Nothing, "should have a valid ie8acceleratorInfo.")

        assert(Not info.activities Is Nothing AndAlso info.activities.size() > 0, "no valid activities.")
        Dim i As Int64
        For i = 0 To info.activities.size() - 1
            Dim activity As xmlcontrol = Nothing
            activity = context.createControl(New xmlcontrol(Nothing, "activity"))
            activity.attributes("category") = info.activities(i).category
            root.addchild(activity)

            actions(activity, context, info.activities(i), baseUrl)
        Next
    End Sub

    'based on http://www.microsoft.com/schemas/openservicedescription/1.0
    'but the link has already broken
    'surely it's not good to generate a full page in processor, but it's easy to move it to a control
    'as this is not useful in other context, so leave it here now
    'and according to design, we need a context here
    Protected Overloads Overrides Function processImpl(ByVal context As httpContext, _
                                                       ByRef control As xmlcontrol) As Boolean
        assert(Not context Is Nothing, "should have a valid httpContext.")

        control = context.createControl(New xmlpage())

        Dim baseUrl As String = Nothing
        baseUrl = strleft(context.url().full, _
                          strlen(context.url().full) - strlen(pathidentity()) + strlen(network.pathSeperator))

        Dim service As xmlcontrol = Nothing
        service = context.createControl(New xmlcontrol(Nothing, "openServiceDescription"))
        service.attributes("xmlns") = "http://www.microsoft.com/schemas/openservicedescription/1.0"
        control.addchild(service)

        Dim homepageUrl As xmlcontrol = Nothing
        homepageUrl = context.createControl(New xmlcontrol(Nothing, "homepageUrl"))
        homepageUrl.text = baseUrl
        service.addchild(homepageUrl)

        Dim display As xmlcontrol = Nothing
        display = context.createControl(New xmlcontrol(Nothing, "display"))
        service.addchild(display)

        Dim name As xmlcontrol = Nothing
        name = context.createControl(New xmlcontrol(Nothing, "name"))
        name.text = info.name
        display.addchild(name)

        Dim icon As xmlcontrol = Nothing
        icon = context.createControl(New xmlcontrol(Nothing, "icon"))
        icon.text = baseUrl + info.icon
        display.addchild(icon)

        Dim description As xmlcontrol = Nothing
        description = context.createControl(New xmlcontrol(Nothing, "description"))
        description.text = info.description
        display.addchild(description)

        activities(service, context, info, baseUrl)
        Return True
    End Function

    Public Sub New(ByVal inputInfo As ie8acceleratorInfo)
        MyBase.new()
        copy(info, inputInfo)
    End Sub
End Class
